var tabsHeight = 0;
//标识标签页是否初次打开，如果是，不发送选中事件
var tabsAddFlag = false;
function resetTabsHeight() {
    var windowHeight;
    if (self.innerHeight) { // all except Explorer
        windowHeight = self.innerHeight;
    } else {
        if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
            windowHeight = document.documentElement.clientHeight;
        } else {
            if (document.body) { // other Explorers
                windowHeight = document.body.clientHeight;
            }
        }
    }

    tabsHeight = windowHeight - 33;  //27 + 2 + 4
}
function addpage(module,name, url, nodeId) {
    var title = name + " <input type='hidden' value='" + module+"-"+nodeId + "'/>";
    var isExist = $('#tt').tabs('exists', title);
    if (isExist) {
        $('#tt').tabs('select', title);
        var selectedFrame = $('#tt').tabs('getTab', title).find("iframe").get(0);
        if (selectedFrame != null) {
            selectedFrame.src = selectedFrame.src;
        }
        return;
    }

    var tabs = $('#tt').tabs("tabs");
    if (tabs.length > 20) {
        alert("标签页数目已达上限");
        return;
    }

    tabsAddFlag = true;
    $('#tt').tabs('add', {
        title: title,
        content: '<iframe src="' + url + '" width="100%" height="' + tabsHeight + '" marginwidth="3px" marginheight="0" frameborder="0" border="0" framespacing="0" scrolling="auto"></iframe>',
        closable: true
    });
}

function tabsSelectEventHandler(title){
    if(tabsAddFlag){
        tabsAddFlag = false;
        return;
    }else{
        var inputStr=title.substring(title.split(" ")[0].length);
        var titleInfo=$(inputStr).attr("value").split("-");
        window.parent.reFreshProjectShow(titleInfo[0], titleInfo[1]);
    }
}
/*//页面信息提示轮询查询

var intervalSecond = 45000; //间隔时间(毫秒)
var messagerOutTime = 15000; //消息提示框超时时间(毫秒)

function queryDatas(){
    setTimeout(intervalQueryDatas, intervalSecond);
}*/
/*function intervalQueryDatas() {
    var userId = $("#userId").val();
    var newCarPath = CAR_PATH.substring(0,CAR_PATH.length -1);
    $.ajax({
        type: 'GET',
        url: newCarPath + ";remoteClientMark=true;jsessionid=null/messageReceive/query.do_",
        data: {userId:userId},
        dataType: 'json',
        cache:false,
        async: true,
        error: function () {
//                console.log("系统出现异常，操作失败，稍后会自动重试.");
            queryDatas();
        },
        success: function (respData) {
            if (respData != null && respData != "" && respData.length > 0) {
                querysuccessFun(respData);
            }

            queryDatas();
        }
    });
}*/

var idArray = [];
var msgTile = "";
var msgContent = "";
function querysuccessFun(respData) {
    var showData = respData[0];
    idArray[0] = showData.messageId;
    msgTile = showData.subject;
    msgContent = showData.content;
    var showTile = msgTile;
    if (showTile.length > 30) {
        showTile = showTile.substr(0, 27) + "……";
    }
    var showContent = msgContent;
    if (showContent.length > 150) {
        showContent = showContent.substr(0, 150) + "……" + "<br/><span onclick='showMessageDetail()'><b>查看详细</b></span>"
    }
    showContent = "<span style='word-wrap:break-word;padding: 2px'>" + showContent + "</span>";
    $.messager.show({
        width: 320,
        height: 100,
        title: showTile,
        msg: showContent,
        timeout: messagerOutTime,
        showType: 'slide',
        onBeforeClose: setMessageIsRead
    });
}
;

function setMessageIsRead() {
    $.ajax({
        type: 'POST',
        url: CAR_PATH + "/messageReceive/hasRead.do_",
        data: $.toJSON(idArray),
        contentType: "application/json;charset=utf-8",
        dataType: 'json',
        error:function (XMLHttpRequest, textStatus, errorThrown) {
        },
        success: function(data,status,jqXHR) {
            var heads = jqXHR.getAllResponseHeaders();
            if(heads.indexOf("CAR-ErrorInfo")!=-1){
                alert("登录超时请重试");
                window.parent.location = CAR_PATH + "login.jsp";
            }
        }
    });
}

function showMessageDetail() {
    $('#win').window({
        width: 400,
        height: 200,
        modal: true,
        minimizable: false,
        collapsible: false,
        title: msgTile,
        content: "<span style='word-wrap:break-word;padding: 2px'>" + msgContent + "</span>",
        onClose: setMessageIsRead
    });
}

$(function () {
    resetTabsHeight();
    $('#tt').tabs({
        border: false,
        fit: true,
        tools: [
            {
                iconCls: 'icon-cancel',
                handler: function () {
                    var tabs = $('#tt').tabs("tabs");
                    var tabTitle = "";
                    while (tabs.length > 1) {
                        tabTitle = tabs[1].panel("options").title;
//                        if(tabTitle != "首页")
                        $('#tt').tabs("close", tabTitle);
                        tabs = $('#tt').tabs("tabs");
                    }
                }
            }
        ],
        onSelect:tabsSelectEventHandler
    });

    $(window).resize(function () {
        resetTabsHeight();
        $("iframe").css("height", tabsHeight);
    });

    queryDatas();
})